{extend name="app/admin/view/base.html"/}
{block name="resources"}
<style>
	.ns-form-row{margin-top: 0; margin-left: 250px;}
	.layui-form-label{
		width:230px;
	}
	.ns-word-aux {
		margin-left: 230px;
	}
</style>
{/block}
{block name="main"}
<div class="layui-form">

	<div class="layui-card ns-card-common ns-card-brief">
		<div class="layui-card-header">
			<span class="ns-card-title">开放平台设置</span>
		</div>

		<div class="layui-card-body">

			<div class="layui-form-item">
				<label class="layui-form-label">APPID：</label>
				<div class="layui-input-block">
					<input type="text" name="appid" autocomplete="off" value="{$config.value.appid ?? ''}" class="layui-input ns-len-long">
				</div>
				<div class="ns-word-aux">微信第三方平台AppId</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">APPSECRET：</label>
				<div class="layui-input-block">
					<input type="text" name="secret" autocomplete="off" value="{$config.value.secret ?? ''}" class="layui-input ns-len-long">
				</div>
				<div class="ns-word-aux">微信第三方平台AppSecret</div>
			</div>
		</div>
	</div>

	<div class="layui-card ns-card-common ns-card-brief">
		<div class="layui-card-header">
			<span class="ns-card-title">开放平台通信</span>
		</div>

		<div class="layui-card-body">

			<div class="layui-form-item">
				<label class="layui-form-label">授权发起页域名：</label>
				<div class="layui-input-inline" id="url1">
					{$request_scheme}://{$host}
				</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url1')">复制</button>
			</div>

			<!--<div class="layui-form-item">-->
				<!--<label class="layui-form-label">发起授权页的体验URL：</label>-->
				<!--<div class="layui-input-inline" id="url2">-->
					<!--yanshi.niuteam.cn/web/index.php?c=account&a=auth&do=test-->
				<!--</div>-->
				<!--<button class="layui-btn layui-btn-primary" onclick="copy_text('url2')">复制</button>-->
			<!--</div>-->

			<div class="layui-form-item">
				<label class="layui-form-label">授权事件接受URL：</label>
				<div class="layui-input-inline" id="url3">
					{$request_scheme}://{$host}/wxoplatform/api/auth/relate
				</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url3')">复制</button>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color:red">消息校验Token：</span></label>
				<div class="layui-input-inline">
					<input type="text" name="token" autocomplete="off" id="Token" class="layui-input ns-len-long" value="{$config.value.token ?? ''}">
				</div>
				<button class="layui-btn layui-btn-primary" onclick="ns.copy('Token')">复制</button>
				<div class="ns-word-aux">开发者在代替公众号或小程序接收到消息时，用此Token来校验消息。</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label"><span style="color:red">消息加解密Key：</span></label>
				<div class="layui-input-inline">
					<input type="text" autocomplete="off" name="aes_key" id="AESKey" class="layui-input ns-len-long" value="{$config.value.aes_key ?? ''}">
				</div>
				<button class="layui-btn layui-btn-primary" onclick="ns.copy('AESKey')">复制</button>
				<div class="ns-word-aux">在代替公众号或小程序收发消息过程中使用。必须是长度为43位的字符串，只能是字母和数字。</div>
			</div>

			<div class="layui-form-item">
				<label class="layui-form-label">消息与事件接收URL：</label>
				<div class="layui-input-inline" id="url4">
					{$request_scheme}://{$host}/wxoplatform/api/auth/message/appid/$APPID$
				</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url4')">复制</button>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">公众号开发域名：</label>
				<div class="layui-input-inline" id="url5">{$host}</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url5')">复制</button>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">小程序服务器域名：</label>
				<div class="layui-input-inline" id="url5">{$host}</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url5')">复制</button>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label">小程序业务域名：</label>
				<div class="layui-input-inline" id="url5">{$host}</div>
				<button class="layui-btn layui-btn-primary" onclick="copy_text('url5')">复制</button>
			</div>
		</div>
	</div>

	<div class="ns-form-row">
		<button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
	</div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">

    function copyText(text) {
        var textarea = document.createElement("textarea");
        var currentFocus = document.activeElement;
        document.body.appendChild(textarea);
        textarea.value = text;
        textarea.focus();
        if (textarea.setSelectionRange)
            textarea.setSelectionRange(0, textarea.value.length);
        else
            textarea.select();
        try {
            var flag = document.execCommand("copy");
        } catch(eo){
            var flag = false;
        }
        document.body.removeChild(textarea);
        currentFocus.focus();
        return flag;
    }

    function copy_text(dom){
        var text = $("#"+dom).text();
        var flag = copyText(text);//这个必须在DOM对象的事件线程中执行

        layer.msg(flag ? "复制成功！" : "复制失败！");
    }

    layui.use(['form'], function () {
        var form = layui.form,
            repeat_flag = false; //防重复标识
		form.render();

		form.on('submit(save)', function(data) {
			if (repeat_flag) return;
			repeat_flag = true;

			$.ajax({
				type: "post",
				url: "{:addon_url('wxoplatform://admin/oplatform/config')}",
				dataType: "JSON",
				data: data.field,
				success: function(data) {
					repeat_flag = false;
					layer.msg(data.message);
				}
			});
		});

	});
	
</script>
{/block}